$(document).ready(function() {
        var arr;
	if (window.DeviceMotionEvent){
                $.ajax({
                    url:"getGifts.php",
                    method:"get",
                    cache:false,
                    async:false,
                    data:{id:1},
                    success:function(data){
                        arr = JSON.parse(data);
                        
                    }
                });
		var speed = 25;
		var audio = document.getElementById("shakemusic");
		var openAudio = document.getElementById("openmusic");
		var u = navigator.userAgent;
		var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;
		var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
		var x = t = z = lastX = lastY = lastZ = 0;
		//var arr = [{"gift":"苹果"},{"gift":"梨子"},{"gift":"橘子"},{"gift":"红龙果"},{"gift":"车厘子"},{"gift":"柚子"},{"gift":"西瓜"}];
		var flag = true;
		var randomNum = null;
		$("#btn_close").click(function(){
			$('.red-yzj').html("");
			$('.red-tc').css('display', 'none');
		});
		//预加摇一摇声音
		var shakeAudio = new Audio();
		var shakeAudio2 = new Audio();
		shakeAudio2.src =  'music/red-01.mp3';
		shakeAudio.src = 'music/red-02.mp3';
		var shake_options = {
			preload  : 'auto'
		};
		var shake_options2 = {
			preload  : 'auto'
		};
		$(document).on("touchstart", function() {
			shakeAudio.load();
			shakeAudio2.load();
			//shakeAudio.play()
		});
		for(var key in shake_options){
			if(shake_options.hasOwnProperty(key) && (key in shakeAudio)){
				shakeAudio[key] = shake_options[key];
			}
		}
		for(var key in shake_options2){
			if(shake_options2.hasOwnProperty(key) && (key in shakeAudio2)){
				shakeAudio2[key] = shake_options2[key];
			}
		}
		window.addEventListener('devicemotion',
			function () {
				if(flag){
					var acceleration = event.accelerationIncludingGravity;
					x = acceleration.x;
					y = acceleration.y;
					if ((Math.abs(x - lastX) > speed || Math.abs(y - lastY) > speed)&& flag) {
						shakeAudio2.play();
						flag = false;
                                                $.ajax({
                                                        url:"getGifts.php",
                                                        method:"get",
                                                        cache:false,
                                                        async:false,
                                                        data:{id:2},
                                                        success:function(data){
                                                            randomNum = parseInt(data);
                                                        }
                                                    });
//						if(arr.length>0){
//							randomNum = parseInt(Math.random()*(arr.length));
//						}
						$('.red-ss').addClass('wobble');
                                                $("#money").html(arr[randomNum].price);
                                                $("#pride_text").html("恭喜你获得"+arr[randomNum].name);
                                                $("#imgSrc").attr("src",arr[randomNum].image);
//                                                if(arr[randomNum] == "再接再厉"){
//                                                    $("#imgSrc").removeAttr("src");
//                                                    $("#imgSrc").attr("src","img/red-bw.png");
//                                                    $("#pride_text").html("哎呀！</br>您的奖品被风刮走了啊");
//                                                }else{
//                                                    $("#pride_text").html("恭喜你获得"+arr[randomNum]);
//                                                }
						setTimeout(function(){
							//audio.pause();
							//openAudio.play();
							shakeAudio2.pause();
							shakeAudio.play();
							$('.red-tc').css('display', 'block');
						}, 1500);
					};
					lastX = x;
					lastY = y;
				}
			}
		,false);
	};
});





